package org.song.videoplayer.media;

import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.app.Context;
import ohos.media.audio.AudioManager;
import ohos.media.player.Player;

import java.util.Map;


/**
 * Created by song on 2017/2/22.
 * Contact github.com/tohodog
 * ijk解码器父类
 */

public abstract class IjkBaseMedia extends BaseMedia implements Player.IPlayerCallback {


    public Player mediaPlayer;

    public IjkBaseMedia(IMediaCallback iMediaCallback) {
        super(iMediaCallback);
    }

    @Override
    public boolean doPrepar(final Context context, final String url, final Map<String, String> headers, Object... objects) {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
        try {
            release();
            mediaPlayer = getMedia(context, url, headers, objects);
            mediaPlayer.setPlayerCallback(this);

            mediaPlayer.setAudioStreamType(AudioManager.AudioVolumeType.STREAM_MUSIC.ordinal());
            mediaPlayer.enableScreenOn(true);
            mediaPlayer.prepare();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            onError(MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNKNOWN);
            return false;
        }
        //}  }).start();

    }

    abstract Player getMedia(Context context, String url, Map<String, String> headers, Object... objects) throws Exception;

    @Override
    public void setSurface(Surface surface) {
        try {
            if (mediaPlayer != null)
                mediaPlayer.setVideoSurface(surface);
            this.surface = surface;
        } catch (Exception e) {
            e.printStackTrace();
            iMediaCallback.onError(this, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNKNOWN);
        }
    }

    @Override
    public void setDisplay(SurfaceOps surfaceHolder) {
        try {
            if (mediaPlayer != null)
                mediaPlayer.setSurfaceOps(surfaceHolder);
            if (surfaceHolder != null)
                this.surface = surfaceHolder.getSurface();
        } catch (Exception e) {
            e.printStackTrace();
            iMediaCallback.onError(this, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNKNOWN);
        }
    }

    @Override
    public void doPlay() {
        if (!isPrepar)
            return;
        mediaPlayer.play();
    }

    @Override
    public void doPause() {
        if (!isPrepar)
            return;
        mediaPlayer.pause();
    }

    @Override
    public void seekTo(int duration) {
        if (!isPrepar)
            return;
        mediaPlayer.rewindTo(duration);
    }

    @Override
    public int getCurrentPosition() {
        if (!isPrepar)
            return 0;
        try {
            return (int) mediaPlayer.getCurrentTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int getDuration() {
        if (!isPrepar)
            return 0;
        try {
            return (int) mediaPlayer.getDuration();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int getVideoHeight() {
        if (!isPrepar)
            return 0;
        return mediaPlayer.getVideoHeight();
    }

    @Override
    public int getVideowidth() {
        if (!isPrepar)
            return 0;
        return mediaPlayer.getVideoWidth();
    }

    @Override
    public boolean isPlaying() {
        if (!isPrepar)
            return false;
        return mediaPlayer.isNowPlaying();
    }

    @Override
    public boolean setVolume(float volume) {

        if (isPrepar)
            mediaPlayer.setVolume(volume);
        return false;
    }

    @Override
    public void release() {

        isPrepar = false;
        this.surface = null;
        if (mediaPlayer != null) {
//            final IMediaPlayer temp = mediaPlayer;
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    if (temp != null) {
//                        temp.release();
//                    }
//                }
//            }).start();
            mediaPlayer.release();
        }
        mediaPlayer = null;

    }

    /////////////以下MediaPlayer回调//////////////
    @Override
    public void onPrepared() {
        isPrepar = true;
        iMediaCallback.onPrepared(this);
    }

    @Override
    public void onMessage(int what, int extra) {
        iMediaCallback.onInfo(this, what, extra);
    }

    @Override
    public void onError(int what, int extra) {
        iMediaCallback.onError(this, what, extra);
        isPrepar = false;
    }

    @Override
    public void onResolutionChanged(int width, int height) {
        iMediaCallback.onVideoSizeChanged(this, width, height);
    }

    @Override
    public void onPlayBackComplete() {
        iMediaCallback.onCompletion(this);
    }

    @Override
    public void onRewindToComplete() {
        iMediaCallback.onSeekComplete(this);
    }

    @Override
    public void onBufferingChange(int percent) {
        iMediaCallback.onBufferingUpdate(this, percent / 100.0f);
    }

    @Override
    public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

    }

    @Override
    public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

    }
}